package com.cnpinxi.controller.admin.product;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.Product;
import com.cnpinxi.domain.ProductEntity;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ProductEntityService;
import com.cnpinxi.service.ProductService;
import com.cnpinxi.service.ProductTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/admin/product")
@Controller
public class AdminProductController extends BaseController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    ProductTypeService productTypeService;

    /**
     * 查询所有产品名称
     * @param request
     * @return
     */
    @RequestMapping("/getProductList")
    public ModelAndView getProductList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/product/getProductList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Product> productList = productService.getProductList(map);
        PageInfo<Product> pageinfo = new PageInfo<>(productList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }


    /**
     * 跳转到产品名称编辑或添加页面
     * @param request
     * @return
     */
    @RequestMapping("/getProduct")
    public ModelAndView getProduct(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/product/getProduct");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtils.isNotBlank(tid)){
            Product product = productService.getProduct(map);
            m.addObject("product",product);
        }
        m.addObject("productType",productTypeService.getProductTypeList(null));

        return m;
    }

    /**
     * 编辑产品名称
     * @param product
     * @return
     */
    @RequestMapping(value = "/getProductSubmit")
    @ResponseBody
    public String getProductSubmit(Product product){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            productService.modifyProduct(product);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 按名称查询该产品
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntityByName", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getProductByName(HttpServletRequest request){
        Map<String,String> map =this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            List<ProductEntity> productEntitys = productEntityService.getProductEntityList(map);
            resultObj.setData(productEntitys);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }


    /**
     * 按名称查机构
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntityByName")
    @ResponseBody
    public String getProductEntityByName(HttpServletRequest request){
        Map<String,String> map =this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
            resultObj.setData(productEntityList);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }
}
